<?php

namespace App\Models;

use Validator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Member extends Model
{
    use SoftDeletes;
    protected $fillable = ['username', 'password', 'nickname', 'email'];
    protected $dateFormat = 'U';

    //关联文章模型
    public function articles()
    {
        return $this->hasMany('App\\Models\\Article', 'member_id', 'id');
    }

    //关联评论
    public function comments()
    {
        return $this->hasMany('App\\Models\\Comment', 'member_id', 'id');
    }

    //添加
    public function add($data)
    {
        $rule = [
            'username' => 'bail|required|unique:members',
            'password' => 'required',
            'nickname' => 'required',
            'email' => 'required|email|unique:members'
        ];

        $msg = [
            'username.required' => '用户名不能为空',
            'username.unique' => '用户名已存在',
            'password.required' => '密码不能为空',
            'nickname.required' => '昵称不能为空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '此邮箱已经注册过'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        }else {
            return '会员添加失败';
        }
    }

    //编辑
    public function edit($data)
    {
        $rule = [
            'oldpass' => 'bail|required',
            'password' => 'required',
            'nickname' => 'required'
        ];

        $msg = [
            'oldpass.required' => '原密码不能为空',
            'password.required' => '新密码不能为空',
            'nickname.required' => '昵称不能为空',
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $memberInfo = $this->find($data['id']);
        if ($data['oldpass'] != $memberInfo['password']) {
            return '原密码不正确';
        }
        $memberInfo->password = $data['password'];
        $memberInfo->nickname = $data['nickname'];
        $result = $memberInfo->save();
        if ($result) {
            return 1;
        }else {
            return '修改失败';
        }
    }

    //会员注册
    public function register($data)
    {
        $rule = [
            'username' => 'bail|required|unique:members',
            'password' => 'required',
            'conpass' => 'required|same:password',
            'nickname' => 'required',
            'email' => 'required|email|unique:members',
            'verify' => 'required|captcha'
        ];

        $msg = [
            'username.required' => '用户名不能为空',
            'username.unique' => '此用户名已存在',
            'password.required' => '密码不能为空',
            'conpass.required' => '确认密码不能为空',
            'conpass.same' => '俩次密码输入不一致',
            'nickname.required' => '昵称不能为空',
            'email.required' => '注册邮箱不能为空',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '此邮箱已注册',
            'verify.required' => '验证码不能为空',
            'verify.captcha' => '验证码不正确'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        }else {
            return '会员注册失败';
        }
    }

    //登录
    public function login($data)
    {
        $rule = [
            'username' => 'bail|required',
            'password' => 'required',
            'verify' => 'required|captcha'
        ];

        $msg = [
            'username.required' => '用户名不能为空',
            'password.required' => '密码不能为空',
            'verify.required' => '验证码不能为空',
            'verify.captcha' => '验证码不正确'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        unset($data['verify']);
        $result = $this->where($data)->first();
        if ($result) {
            $sessionData = [
                'id' => $result->id,
                'nickname' => $result->nickname
            ];
            session(['index' => $sessionData]);
            return 1;
        }else {
            return '用户名或者密码不正确';
        }
    }
}
